package com.xbai.goods.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.goods.pojo.Pref;
import com.xbai.goods.service.PrefService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc PrefController
 * @author jxbai
 * @date 2021/1/28
 */

@RestController
@RequestMapping("/pref")
@CrossOrigin
@RequiredArgsConstructor
public class PrefController {

    private final PrefService prefService;

    /***
     * Pref分页条件搜索实现
     * @param pref {@link Pref}
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Pref pref, @PathVariable  int page, @PathVariable  int size){
        //调用PrefService实现分页条件查询Pref
        PageInfo<Pref> pageInfo = prefService.findPage(pref, page, size);
        return new Result<>(pageInfo);
    }

    /***
     * Pref分页搜索实现
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用PrefService实现分页查询Pref
        PageInfo<Pref> pageInfo = prefService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索Pref数据
     * @param pref {@link Pref}
     * @return {@link Result}
     */
    @PostMapping(value = "/search" )
    public Result<List<Pref>> findList(@RequestBody(required = false) Pref pref){
        //调用PrefService实现条件查询Pref
        List<Pref> list = prefService.findList(pref);
        return new Result<>(list);
    }

    /***
     * 根据ID删除Pref数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用PrefService实现根据主键删除
        prefService.delete(id);
        return new Result();
    }

    /***
     * 修改Pref数据
     * @param pref {@link Pref}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Pref pref, @PathVariable Integer id){
        //设置主键值
        pref.setId(id);
        //调用PrefService实现修改Pref
        prefService.update(pref);
        return new Result();
    }

    /***
     * 新增Pref数据
     * @param pref {@link Pref}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody Pref pref){
        //调用PrefService实现添加Pref
        prefService.add(pref);
        return new Result();
    }

    /***
     * 根据ID查询Pref数据
     * @param id 主键id
     * @return {@link Result< Pref >}
     */
    @GetMapping("/{id}")
    public Result<Pref> findById(@PathVariable Integer id){
        //调用PrefService实现根据主键查询Pref
        Pref pref = prefService.findById(id);
        return new Result<>(pref);
    }

    /***
     * 查询Pref全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<Pref>> findAll(){
        //调用PrefService实现查询所有Pref
        List<Pref> list = prefService.findAll();
        return new Result<>(list) ;
    }
}
